# -*- coding: UTF-8 -*-
import requests
import json
from bean.hot_new import HotNew
from typing import List
import traceback
from news_platform.plat.web_platform import WebPlatform

base_url = "https://www.zhihu.com/question/"
api_url = "https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50&desktop=true"
app_path = "pages/question/index?id={0}"


class ZhiHu(WebPlatform):

    def __init__(self):
        self.name = "知乎热榜"

    def query_hot_news(self):
        result: List[HotNew] = []
        try:
            response = requests.get(api_url)
            data = json.loads(response.content)
            news = data['data']
            serial_no = 0
            for new in news:
                serial_no = serial_no + 1
                title = new['target']['title']
                link = base_url + str(new['target']['id'])
                ext_info = str(new['detail_text'])
                excerpt = new['target']['excerpt']
                hot_new = HotNew(serial_no, title, link, "2", ext_info, excerpt)
                hot_new.app_path = app_path.format(str(new['target']['id']))
                result.append(hot_new)
        except Exception as e:
            print(e)
            print(traceback.format_exc())
        return result

